from django.contrib.auth.models import Permission, Group
from django.contrib.contenttypes.models import ContentType
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from users.models import User

from meiduo_admin.serializer.permissions import PermissionSerializer, ContentTypeSerializer, GroupSerializer, \
    SimpleSerializer, UserManagerSerializer, GroupSimpleSerializer


class PermissionViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    serializer_class = PermissionSerializer
    queryset = Permission.objects.all().order_by('id')

    def content_type(self, request):
        queryset = ContentType.objects.all()
        serializer = ContentTypeSerializer(queryset, many=True)
        return Response(serializer.data)


# class ContentTypeView(ListAPIView):    #可写在PermissionViewSet视图中
#     pagination_class = None
#     permission_classes = [IsAdminUser]
#     serializer_class = ContentTypeSerializer
#     queryset = ContentType.objects.all()

class GroupViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    serializer_class = GroupSerializer
    queryset = Group.objects.all()

    def simple(self, request):
        queryset = Permission.objects.all()
        serializer = SimpleSerializer(queryset, many=True)
        return Response(serializer.data)

class UserManagerViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]
    serializer_class = UserManagerSerializer
    queryset = User.objects.filter(is_staff=True)

    def GroupSimple(self,request):
        queryset=Group.objects.all()
        serializer=GroupSimpleSerializer(queryset,many=True)
        return Response(serializer.data)

    # def PermissionSimpleView(self,request):
    #     queryset=Permission.objects.all()
    #     serializer=SimpleSerializer(queryset,many=True)


